import { storeFreeze } from 'ngrx-store-freeze';
import { StoreModule, MetaReducer, ActionReducer, createFeatureSelector, createSelector } from '@ngrx/store';
import { NgModule } from '@angular/core';
import { ActionReducerMap } from '@ngrx/store/src/models';
import { environment } from '../../environments/environment';
import {
    StoreRouterConnectingModule,
    RouterStateSerializer,
} from '@ngrx/router-store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { Auth } from '../domain/index';

import * as fromQuote from './quote.reducer';
import * as fromAuth from './auth.reducer';
import * as fromProjects from './projects.reducer';
import * as fromTaskList from './task-list.reducer';
import * as fromTask from './task.reducer';
import * as fromUser from './user.reducer';
export interface State {
    quote: fromQuote.State;
    auth: Auth;
    projects: fromProjects.State;
    taskList: fromTaskList.State;
    task: fromTask.State;
    user: fromUser.State;
}

export const reducers: ActionReducerMap<State> = {
    quote: fromQuote.reducer,
    auth: fromAuth.reducer,
    projects: fromProjects.reducer,
    taskList: fromTaskList.reducer,
    task: fromTask.reducer,
    user: fromUser.reducer
};


// console.log all actions 对 actions 进行打印输出
export function logger(reducer: ActionReducer<State>): ActionReducer<State> {
    return function (state: State, action: any): State {
        console.log('state', state);
        console.log('action', action);

        return reducer(state, action);
    };
}
export const metaReducers: MetaReducer<State>[] = !environment.production
    ? [logger, storeFreeze]
    : [];


// 各种reducers
// quote
export const getQuoteState = createFeatureSelector<fromQuote.State>('quote');
export const getQuote = createSelector(getQuoteState, fromQuote.getQuote);

// auth
export const getAuthState = createFeatureSelector<Auth>('auth');

// project
export const getProjectsState = createFeatureSelector<fromProjects.State>('projects');
export const getProjects = createSelector(getProjectsState, fromProjects.getAll);

// taskList
export const getTaskListState = createFeatureSelector<fromTaskList.State>('taskList');
export const getTaskLists = createSelector(getTaskListState, fromTaskList.getSelected);

// task
export const getTaskState = createFeatureSelector<fromTask.State>('task');
export const getTasks = createSelector(getTaskState, fromTask.getTasks);

// user
export const getUserState = createFeatureSelector<fromUser.State>('task');
export const getUsers = createSelector(getUserState, fromUser.getUsers);
export const getUserEntities = createSelector(getUserState, fromUser.getEntities);

export const getTasksWithOwners = createSelector(getTasks, getUserEntities, (tasks, userEntities) => {
    return tasks.map(task => {
        return {
            ...tasks,
            owner: userEntities[task.ownerId],
            participants: task.participantIds.map(id => userEntities[id])
        };
    });
});

export const getTaskByList = createSelector(getTaskLists, getTasksWithOwners, (lists, tasks) => {
    return lists.map(list => {
        return {
            ...list,
            tasks: tasks.filter(task => task['taskListId'] === this.id)
        };
    });
});

export const getProjectUsers = (projectId: string) => createSelector(getProjectsState, getUserEntities, (state, entities) => {
    return state.entities[projectId].members.map(id => entities[id]);
});

@NgModule({
    declarations: [],
    imports: [
        // StoreModule.forRoot(reducers, { metaReducers }),
        StoreModule.forRoot(reducers),
        StoreRouterConnectingModule.forRoot({
            stateKey: 'router',
        }),
        StoreDevtoolsModule.instrument()
    ]
})
export class AppStoreModule { }
